﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace GameSPK
{
    public class CauHoiDAO
    {

        public static int tongCauHoiPhanThi(int phanthi, string cuocthi)
        {
            int socauhoi = 0;
            DBTroChoiDataContext db = new DBTroChoiDataContext();
            var soch = (from pt in db.PhanThi_CuocThis
                        where pt.MaPT == phanthi && pt.MaCuocChoi == cuocthi
                        select pt).Single();
            socauhoi = (int)soch.SoCauHoi;
            
            return socauhoi;        
        }
        public static bool luuCauHoi(General cuocthi,int phanthi,string noidung, string dapan, bool isXML)
        {
            bool result = false;
            DBTroChoiDataContext db = new DBTroChoiDataContext();
            try
            {
                GoiCauHoi goiCauHoi = new GoiCauHoi();
                goiCauHoi.MaGoi = Helper.GenerateKey();
                goiCauHoi.NgayNhap = DateTime.Now;
                db.GoiCauHois.InsertOnSubmit(goiCauHoi);
                db.SubmitChanges();

                var chuDe = (from cd in db.ChuDes
                             select cd).First();
                CauHoi cauhoi = new CauHoi();
                cauhoi.MaCauHoi = Helper.GenerateKey();
                cauhoi.CauHoi1 = noidung;
                cauhoi.DapAnDung = dapan;
                cauhoi.XML = isXML;
                cauhoi.MaCD = chuDe.MaCD;
                cauhoi.MaPT = phanthi;
                cauhoi.MaGoi = goiCauHoi.MaGoi;

                db.CauHois.InsertOnSubmit(cauhoi);
                db.SubmitChanges();

                CauHoi_CuocChoi cauhoi_cuocchoi = new CauHoi_CuocChoi();
                cauhoi_cuocchoi.MaCauHoi = cauhoi.MaCauHoi;
                cauhoi_cuocchoi.MaCuocChoi= cuocthi.MaCuocChoi;
                cauhoi_cuocchoi.Chon = false;
                db.CauHoi_CuocChois.InsertOnSubmit(cauhoi_cuocchoi);
                db.SubmitChanges();
                result = true;
            }
            catch
            {
                result = false;
            }
            return result;
        }

        public static bool luuCauHoi(General cuocthi, int phanthi, ChuDe chuDe, string noidung, string dapan,int extendMa, bool isXML)
        {
            bool result = false;
            DBTroChoiDataContext db = new DBTroChoiDataContext();
            try
            {
                GoiCauHoi goiCauHoi = new GoiCauHoi();
                goiCauHoi.MaGoi = Helper.GenerateKey() + "" + extendMa;
                goiCauHoi.NgayNhap = DateTime.Now;
                db.GoiCauHois.InsertOnSubmit(goiCauHoi);
                db.SubmitChanges();

                
                CauHoi cauhoi = new CauHoi();
                cauhoi.MaCauHoi = Helper.GenerateKey() + "" + extendMa;
                cauhoi.CauHoi1 = noidung;
                cauhoi.DapAnDung = dapan;
                cauhoi.XML = isXML;
                cauhoi.MaCD = chuDe.MaCD;
                cauhoi.MaPT = phanthi;
                cauhoi.MaGoi = goiCauHoi.MaGoi;

                db.CauHois.InsertOnSubmit(cauhoi);
                db.SubmitChanges();

                CauHoi_CuocChoi cauhoi_cuocchoi = new CauHoi_CuocChoi();
                cauhoi_cuocchoi.MaCauHoi = cauhoi.MaCauHoi;
                cauhoi_cuocchoi.MaCuocChoi = cuocthi.MaCuocChoi;
                cauhoi_cuocchoi.Chon = false;
                db.CauHoi_CuocChois.InsertOnSubmit(cauhoi_cuocchoi);
                db.SubmitChanges();
                result = true;
            }
            catch
            {
                result = false;
            }
            return result;
        }
        
        public static int isNhapXongCauHoi(int phanthi, string cuocthi)
        {
            int conlai = 0;
            int socauhoi;
            DBTroChoiDataContext db = new DBTroChoiDataContext();
            var soch = (from pt in db.PhanThi_CuocThis
                        where pt.MaPT == phanthi && pt.MaCuocChoi == cuocthi
                        select pt).Single();
            socauhoi = (int)soch.SoCauHoi;
            int socauhoidanhap;

            try
            {

                socauhoidanhap = (from ch_nhap in db.CauHois
                                  join chdanhap in db.CauHoi_CuocChois on ch_nhap.MaCauHoi equals chdanhap.MaCauHoi
                                  where ch_nhap.MaPT == phanthi && chdanhap.MaCuocChoi == cuocthi

                                  select ch_nhap)
                                  .Count();
            }
            catch
            {
                socauhoidanhap = 0;
            }
            conlai = socauhoi - socauhoidanhap;


            return conlai;
        }
        public static bool SaveCauHoiPhanThi(ObservableCollection<ICauHoi> taphop, String machoi,int goi)
        {
            bool kq = true;
            if (taphop == null)
                return kq;
            DBTroChoiDataContext db = new DBTroChoiDataContext();
            foreach (dataCauHoi dataCH in taphop)
            {
                if (dataCH.IsEnabled != true)
                    continue;
                CauHoi_CuocChoi ch_cch = new CauHoi_CuocChoi();
                var cauhoiupdate = (from chupdate in db.CauHois
                                    where chupdate.MaCauHoi == dataCH.MaCauHoi
                                    select chupdate).Single();
                cauhoiupdate.CauHoi1 = dataCH.NoiDung;
                cauhoiupdate.DapAnDung = dataCH.DapAn;
                ch_cch.MaCauHoi = dataCH.MaCauHoi;
                ch_cch.MaCuocChoi = machoi;
                ch_cch.Chon = false;
                ch_cch.Goi = goi;
                db.CauHoi_CuocChois.InsertOnSubmit(ch_cch);
                
            }
            try
            {
                db.SubmitChanges();
            }
            catch
            {
                return false;
            }
            kq = true;
            return kq;
        }

        public static bool UpdateCauHoiPhanThi(ObservableCollection<ICauHoi> taphop, String machoi)
        {
            bool kq = true;
            if (taphop == null)
                return kq;
            DBTroChoiDataContext db = new DBTroChoiDataContext();
            foreach (dataCauHoi dataCH in taphop)
            {
                if (dataCH.IsEnabled == true)
                {
                    var cauhoiupdate = (from chupdate in db.CauHois
                                        where chupdate.MaCauHoi == dataCH.MaCauHoi
                                        select chupdate).Single();
                    cauhoiupdate.CauHoi1 = dataCH.NoiDung;
                    cauhoiupdate.DapAnDung = dataCH.DapAn;
                    continue;
                }
                var cauhoi_cuocchoi = (from chch in db.CauHoi_CuocChois
                                       where chch.MaCauHoi == dataCH.MaCauHoi
                                       select chch).Single();
                
                db.CauHoi_CuocChois.DeleteOnSubmit(cauhoi_cuocchoi);
            }
            try
            {
                db.SubmitChanges();
            }
            catch
            {
                return false;
            }
            kq = true;
            return kq;
        }
     
        
        
        public static ObservableCollection<ICauHoi> getFromChuDe(ChuDe chuDe, int phanthi, String cuocchoi)
        {
            ObservableCollection<ICauHoi> taphopcauhoi = new ObservableCollection<ICauHoi>();
            DBTroChoiDataContext db = new DBTroChoiDataContext();
            var cauhoidachon = from ch in db.CauHoi_CuocChois
                               where ch.MaCuocChoi == cuocchoi
                               select ch;
            String [] list = new String[cauhoidachon.Count()];
            int index = 0;
            foreach(CauHoi_CuocChoi chch in cauhoidachon)
            {
                list[index] = chch.MaCauHoi;
                index++;
            }
            var cauhoidb = from ch in db.CauHois
                           where (ch.MaCD == chuDe.MaCD && ch.MaPT == phanthi 
                                           )
                           select ch;
            bool flag;
            foreach (CauHoi CH in cauhoidb)
            {
                flag = true;
                for (int i = 0; i < index; i++)
                {
                    if (CH.MaCauHoi == list[i])
                    {
                        flag = false;
                        break;
                    }
                }
                if (!flag)
                    continue;
                dataCauHoi dulieu = new dataCauHoi(CH,false);
                taphopcauhoi.Add(dulieu);
            }
            return taphopcauhoi;
        }
        
        public static ObservableCollection<ICauHoi> getFromChuDe(ChuDe chuDe, int phanthi, String cuocchoi, int goicauhoi)
        {
            ObservableCollection<ICauHoi> taphopcauhoi = new ObservableCollection<ICauHoi>();
            DBTroChoiDataContext db = new DBTroChoiDataContext();
            var cauhoidachon = from ch in db.CauHoi_CuocChois
                               where ch.MaCuocChoi == cuocchoi
                               select ch;
            String[] list = new String[cauhoidachon.Count()];
            int index = 0;
            foreach (CauHoi_CuocChoi chch in cauhoidachon)
            {
                list[index] = chch.MaCauHoi;
                index++;
            }
            var cauhoidb = from ch in db.CauHois
                           where (ch.MaCD == chuDe.MaCD && ch.MaPT == phanthi
                                           )
                           select ch;
            bool flag;
            foreach (CauHoi CH in cauhoidb)
            {
                flag = true;
                for (int i = 0; i < index; i++)
                {
                    if (CH.MaCauHoi == list[i])
                    {
                        flag = false;
                        break;
                    }
                }
                if (!flag)
                    continue;
                dataCauHoi dulieu = new dataCauHoi(CH, false);
                taphopcauhoi.Add(dulieu);
            }
            return taphopcauhoi;
        }

        public static int getCauHoiDaNhap(int phanthi, String cuocchoi, int goi)
        {

            DBTroChoiDataContext db = new DBTroChoiDataContext();
            var cauhoidb = from ch in db.CauHois
                           join ch_dc in db.CauHoi_CuocChois
                           on ch.MaCauHoi equals ch_dc.MaCauHoi
                           where ((ch_dc.MaCuocChoi == cuocchoi) && ch.MaPT == phanthi && ch_dc.Goi == goi)
                           select ch;
            return cauhoidb.Count();
        }
        public static ObservableCollection<ICauHoi> getDaChon(String cuocchoi, int phanthi, int goi)
        {
            ObservableCollection<ICauHoi> taphopcauhoi = new ObservableCollection<ICauHoi>();
            DBTroChoiDataContext db = new DBTroChoiDataContext();
            try
            {

                var cauhoidb = from ch in db.CauHois
                               join ch_dc in db.CauHoi_CuocChois
                               on ch.MaCauHoi equals ch_dc.MaCauHoi
                               where ((ch_dc.MaCuocChoi == cuocchoi) && ch.MaPT == phanthi && ch_dc.Goi == goi)
                               select ch;
                foreach (CauHoi CH in cauhoidb)
                {
                    dataCauHoi dulieu = new dataCauHoi(CH, true);
                    taphopcauhoi.Add(dulieu);
                }
            }
            catch
            {
                return null;
            }
            return taphopcauhoi;
        }
    }
}
